package com.java.manage.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.java.base.service.HttpAPIService;
import com.java.manage.mapper.TbItemMapper;
import com.java.manage.pojo.TbItem;
import com.java.manage.pojo.TbItemDesc;
import com.java.manage.pojo.TbItemParamItem;
import com.java.manage.service.PropertieService;
import com.java.manage.service.TbItemDescService;
import com.java.manage.service.TbItemParamItemService;
import com.java.manage.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName TbItemServiceImpl
 * @Description 商品服务
 * @Author Trigger Wu
 * @Date 2018/9/716:10
 * @Version 1.0
 **/
@Service
public class TbItemServiceImpl extends ServiceImpl<TbItemMapper,TbItem> implements TbItemService {
    @Autowired
    private TbItemDescService descService;
    @Autowired
    private HttpAPIService apiService;
    @Autowired
    private PropertieService propertieService;


    @Transactional
    public Boolean saveItem(TbItem tbItem, String desc) {
        tbItem.setStatus(0);
        tbItem.setId(null);//防止注入的BUG
        Date d = new Date();
        tbItem.setCreated(d);
        tbItem.setUpdated(d);
        boolean result1 =  super.insert(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(d);
        tbItemDesc.setUpdated(d);
        boolean result2 = descService.insert(tbItemDesc);
        return result1 == true && result2 == true;
    }
    /*
     *  修改商品参数
     * 
     * @author Tigger Wu
     * @date 2018/9/10 15:35
     * @param []
     * @return com.java.base.pojo.EasyUIResult
     */
    @Autowired
    private TbItemParamItemService itemParamItemService;

    @Transactional
    public void updateItem(TbItem item,String desc,String itemParams){
        //修改item信息
        item.setStatus(null);//防止sql注入
        super.updateById(item);
        //修改tbitemparam信息
        TbItemParamItem tbitemparam = new TbItemParamItem();
        tbitemparam.setId(item.getId());
        tbitemparam.setParamData(itemParams);
        //更新规格参数
        itemParamItemService.updateById(tbitemparam);
        //修改itemDesc信息
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        Date date = new Date();
        itemDesc.setUpdated(date);
        descService.updateById(itemDesc);
        try{
            //向其他系统通知该商品已经更新
            String url = propertieService.CQZG_WEB_URL+"/rest/item/cache/"+item.getId();
            this.apiService.doPost(url);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
